﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using CongregacaoOs.Data;

namespace CongregacaoOs.Business
{
    [System.ComponentModel.DataObject]
    public class ccRegionalBus  :IDisposable
    {

        public void Dispose() { }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool RegionalSaveOrUpdate(ccRegional regional)
        {
            if (regional.nome == null) { throw new Exception("A Regional não foi informado"); }
            if (regional.idEstado == null) { throw new Exception("O Estado não foi informado"); }

            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccRegionalSave";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDREGIONAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (regional.idRegional != 0)
                            param.Value = regional.idRegional;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NOME", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (regional.nome != null)
                            param.Value = regional.nome;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDESTADO", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (regional.idEstado != 0)
                            param.Value = regional.idEstado;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDPAIS", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (regional.idPais != 0)
                            param.Value = regional.idPais;
                        else
                            param.Value = DBNull.Value;

                        using (conn)
                        {
                            if (conn.State == ConnectionState.Closed)
                                conn.Open();

                            cmd.ExecuteNonQuery();
                            return true;
                        }
                    }
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<ccRegionalSelect_Result> RegionalSelect(ccRegional regional)
        {
            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccRegionalSelect1";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDREGIONAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (regional.idRegional != 0 && regional.idRegional != null)
                            param.Value = regional.idRegional;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NOME", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (regional.nome != "" && regional.nome != null)
                            param.Value = regional.nome;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDESTADO", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (regional.idEstado != 0 && regional.idEstado != null)
                            param.Value = regional.idEstado;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDPAIS", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (regional.idPais != 0 && regional.idPais != null)
                            param.Value = regional.idPais;
                        else
                            param.Value = DBNull.Value;

                        using (conn)
                        {
                            if(conn.State == ConnectionState.Closed) conn.Open();    

                            using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                            {

                                List<ccRegionalSelect_Result> _result = new List<ccRegionalSelect_Result>();
                                while (reader.Read())
                                {
                                    ccRegionalSelect_Result _regional = new ccRegionalSelect_Result();
                                    _regional.idRegional = reader.IsDBNull(reader.GetOrdinal("idRegional")) == false ? reader.GetInt32(reader.GetOrdinal("idRegional")) : 0;
                                    _regional.regional = reader.IsDBNull(reader.GetOrdinal("regional")) == false ? reader.GetString(reader.GetOrdinal("regional")) : "";
                                    _regional.idPais = reader.IsDBNull(reader.GetOrdinal("idPais")) == false ? reader.GetInt32(reader.GetOrdinal("idPais")) : 0;
                                    _regional.pais = reader.IsDBNull(reader.GetOrdinal("pais")) == false ? reader.GetString(reader.GetOrdinal("pais")) : "";
                                    _regional.idEstado = reader.IsDBNull(reader.GetOrdinal("idEstado")) == false ? reader.GetInt32(reader.GetOrdinal("idEstado")) : 0;
                                    _regional.estado = reader.IsDBNull(reader.GetOrdinal("estado")) == false ? reader.GetString(reader.GetOrdinal("estado")) : "";
                                    _result.Add(_regional);
                                }


                                return _result;
                            }
                        }


                    }
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }

    
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void RegionalDelete(int id)
        {
            try
            {
                using (CcbOrdemServico objDB = new CcbOrdemServico())
                {
                    objDB.ccRegionalDelete(id);
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }
    }
}
